package com.cnh.framework.util;

import org.apache.commons.lang3.StringUtils;

/**
 * @author cnh
 * @create 2019-02-14 11:30
 * 说明：
 */
public class CastUtil {

    /**
     * 转为String类型，默认值为空
     * @param object
     * @return
     */
    public static String castString(Object object) {
        return CastUtil.castString(object, "");
    }

    /**
     * 转为String类型，可指定默认值
     * @param object
     * @param defaultValue
     * @return
     */
    public static String castString(Object object, String defaultValue) {
        return object != null ? String.valueOf(object) : defaultValue;
    }

    /**
     * 转为double型，默认值为0
     * @param object
     * @return
     */
    public static double castDouble(Object object) {
        return CastUtil.castDouble(object, 0);
    }

    /**
     * 转为double型，可指定默认值
     * @param object
     * @param defaultValue
     * @return
     */
    public static double castDouble(Object object, double defaultValue){
        double doubleValue = defaultValue;
        if(object != null){
            String str = castString(object);
            if(StrUtil.isNotEmpty(str)){
                try {
                    doubleValue = Double.valueOf(str);
                } catch (NumberFormatException e) {
                    doubleValue = defaultValue;
                }
            }
        }

        return doubleValue;
    }


    /**
     * 转为long型，默认值为0
     * @param object
     * @return
     */
    public static long castLong(Object object) {
        return CastUtil.castLong(object, 0);
    }

    /**
     * 转为long型，可指定默认值
     * @param object
     * @param defaultValue
     * @return
     */
    public static long castLong(Object object, long defaultValue) {
        long longValue = defaultValue;
        if (object != null) {
            String strValue = castString(object);
            if (StrUtil.isNotEmpty(strValue)) {
                try {
                    longValue = Long.valueOf(strValue);
                } catch (NumberFormatException e) {
                    longValue = defaultValue;
                }
            }
        }
        return longValue;
    }
    /**
     * 转为int型，默认值为0
     * @param object
     * @return
     */
    public static int castInt(Object object) {
        return CastUtil.castInt(object, 0);
    }
    /**
     * 转为int型，可指定默认值
     * @param object
     * @param defaultValue
     * @return
     */
    public static int castInt(Object object, int defaultValue) {
        int intValue = defaultValue;
        if (object != null) {
            String strValue = castString(object);
            if (StrUtil.isNotEmpty(strValue)) {
                try {
                    intValue = Integer.valueOf(strValue);
                } catch (NumberFormatException e) {
                    intValue = defaultValue;
                }
            }
        }
        return intValue;
    }
    /**
     * 转为boolean型，默认值为false
     * @param object
     * @return
     */
    public static boolean castBoolean(Object object) {
        return CastUtil.castBoolean(object, false);
    }

    /**
     * 转为boolean型，可指定默认值
     * @param object
     * @param defaultValue
     * @return
     */
    public static boolean castBoolean(Object object, boolean defaultValue) {
        boolean booleanValue = defaultValue;
        if (object != null) {
            String strValue = castString(object);
            if (StrUtil.isNotEmpty(strValue)) {
                try {
                    booleanValue = Boolean.valueOf(strValue);
                } catch (NumberFormatException e) {
                    booleanValue = defaultValue;
                }
            }
        }
        return booleanValue;
    }
}
